#define Adc_sensor_Variable

#include "userDef.h"

temp_typeDef temp;

void ADConvert(void) /* called in SysTime.c per 10ms */
{
    static U8 count_adc = 0; /* ad采样累计次数 */

    static U16 adRoomSum = 0; /* ad采样值总和...环温 */
    static U16 adTubeSum = 0; /* ad采样值总和...管温 */

    U16 get_value_room[10];
    U16 get_value_tube[10];

    count_adc++;

    adRoomSum += ADC_Converse(ADC_CHANNEL_0, 10, get_value_room) >> 2; /* 获取AD采样值 */
    adTubeSum += ADC_Converse(ADC_CHANNEL_1, 10, get_value_tube) >> 2; /* 获取AD采样值 */

    if (count_adc >= 20) // per 20 * 10ms = 200ms
    {
        temp.room = ADCal(adRoomSum, count_adc) + 200; /* 转换AD采样值 */

        temp.tube = ADCal(adTubeSum, count_adc) + 200; /* 转换AD采样值 */

#ifndef KONGKE
        if (f_ADCEnd == ON)
        {
            tempErrCheck(adRoomSum, adTubeSum, count_adc);
        }
#endif

        adRoomSum = 0;
        adTubeSum = 0;
        count_adc = 0;

        TempJudge(); /* 转换温度值 */
    }
}

U16 ADCal(U16 ADSum, U8 count) /* ad值转换温度值 */
{
    U8 i = 0;
    U16 ADVal, ADSubVal1, ADSubVal2;
    U16 tempVal;

    ADVal = ADSum / count;

    if (ADVal <= TempTab[0]) // 只检测-20以上的温度
    {
        tempVal = 0;
    }
    else
    {
        for (i = 0; TempTab[i] < ADVal; i++)
        {
            ADSubVal1 = ADVal - TempTab[i];
        }

        ADSubVal2 = TempTab[i] - TempTab[i - 1];

        tempVal = ADSubVal1 * 10 / ADSubVal2;

        tempVal += (i - 1) * 10;
    }

    return tempVal;
}

void TempJudge(void) /* 转换温度值 */
{
    /* 环温 */
#ifdef CTRL_485
    if (f_485_room_Err == OFF && (room_mode_wireCtrl == 0x02 || (room_mode_wireCtrl == 0x03 && (e_mode.set == MODEHEAT || e_mode.set == MODEAUTO))))
    {
        temp.room = temp.wireCtrl;

        if (temp.wireCtrl >= 400)
        {
            temp.roomAct = temp.wireCtrl - 400;
        }
        else
        {
            temp.roomAct = 0;
        }

        temp.repairAct = 0;
    }
    else if (temp.IFeel > 0 && temp.IFeel < 600 && timer_IFeelSec > 0)
    {
        temp.room = temp.IFeel + 400;
        temp.roomAct = temp.IFeel;
        temp.repairAct = 0;
    }
    else
    {
        temp.IFeel = 0;
        timer_IFeelSec = 0;

        if (temp.room >= 400)
        {
            temp.roomAct = temp.room - 400;
        }
        else
        {
            temp.roomAct = 0;
        }

        temp.repairAct = temp.repair;
    }
#else
    if (temp.IFeel > 0 && temp.IFeel < 600 && timer_IFeelSec > 0)
    {
        temp.room = temp.IFeel + 400;
        temp.roomAct = temp.IFeel;
        temp.repairAct = 0;
    }
    else
    {
        temp.IFeel = 0;
        timer_IFeelSec = 0;

        if (temp.room >= 400)
        {
            temp.roomAct = temp.room - 400;
        }
        else
        {
            temp.roomAct = 0;
        }

        temp.repairAct = temp.repair;
    }
#endif

    /* 管温 */
    if (temp.tube >= 400)
    {
        temp.tubeAct = temp.tube - 400;
    }
    else
    {
        temp.tubeAct = 0;
    }

    f_ADCEnd = ON;
}

void tempErrCheck(U16 Room, U16 Tube, U8 count) /* 感温包开短路检测，called per 200ms */
{
    static U8 RoomErrTime = 0; // 环温异常计时
    static U8 TubeErrTime = 0; // 管温异常计时

    Room = Room / count;
    Tube = Tube / count;

    if (Room <= 20 || Room >= 1000)
    {
        if (timer_powerOn <= 20)
        {
            if (f_RoomErrQT == 0)
            {
                RoomErrTime++;

                if (RoomErrTime >= 5)
                {
                    f_RoomErrQT = 1;
                }
            }
            else
            {
                RoomErrTime = 0;
            }
        }
        else
        {
            if (f_RoomErr == 0 && b_power.set == ON)
            {
                RoomErrTime++;

                if (RoomErrTime >= 25)
                {
                    f_RoomErr = 1;
                }
            }
            else
            {
                RoomErrTime = 0;
            }
        }
    }
    else
    {
        RoomErrTime = 0;
        f_RoomErr = 0;
    }

    if (Tube <= 20 || Tube >= 1000)
    {
        if (timer_powerOn <= 20)
        {
            if (f_TubeErrQT == 0)
            {
                TubeErrTime++;

                if (TubeErrTime >= 5)
                {
                    f_TubeErrQT = 1;
                }
            }
            else
            {
                TubeErrTime = 0;
            }
        }
        else
        {
            if (f_TubeErr == 0 && b_power.set == ON && DefEndMin == 0)
            {
                TubeErrTime++;

                if (TubeErrTime >= 25)
                {
                    f_TubeErr = 1;
                }
            }
            else
            {
                TubeErrTime = 0;
            }
        }
    }
    else
    {
        f_TubeErr = 0;
        TubeErrTime = 0;
    }
}
